package elmar.swt;

import java.util.Random;

import junit.framework.TestCase;

public class UtilTest extends TestCase {

	public void testLog2() {
		assertEquals(0, Util.log2(1));
		assertEquals(1, Util.log2(2));
		assertEquals(1, Util.log2(3));
		assertEquals(2, Util.log2(5));
		assertEquals(2, Util.log2(4));
		assertEquals(30, Util.log2(Integer.MAX_VALUE));
		assertEquals(29, Util.log2(Integer.MAX_VALUE >> 1));
		assertTrue(Util.log2(-1) < 0);
	}

	public void testIs2N() {
		assertTrue(Util.is2N(1));
		assertTrue(Util.is2N(2));
		assertTrue(Util.is2N(4));
		assertTrue(Util.is2N(8));
		assertTrue(Util.is2N(1 << 30));
		assertFalse(Util.is2N(3));
		assertFalse(Util.is2N(0));
		assertFalse(Util.is2N(6));
		assertFalse(Util.is2N(-1));
		Random ran = new Random();
		for (int i = 0; i < 100; i++) {
			int n = ran.nextInt();
			if(n<3)continue;
			assertEquals(Util.is2N(n), Util.is2N(n+1));
		}
	}

}
